package com.xdh.zzqh.store

import com.xdh.zzqh.R
import com.xdh.zzqh.utils.runWithDb
import com.xdh.zzqh.utils.stringById

class Product(
    val name: String,
    val ty: String,
    val style: String,
    val spec: String,
    val dir: String,
    val hands: String,
    val color: String,
    val price: String
) {

    companion object {
        private const val TB_NAME = "tb_product"
        const val DROP_SQL = "DROP TABLE IF EXISTS $TB_NAME"
        const val CREATE_SQL = "CREATE TABLE IF NOT EXISTS $TB_NAME (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "name TEXT NOT NULL, " +
                "ty TEXT NOT NULL, " +
                "style TEXT NOT NULL, " +
                "spec TEXT NOT NULL, " +
                "dir TEXT NOT NULL, " +
                "hands TEXT NOT NULL, " +
                "color TEXT NOT NULL, " +
                "price TEXT NOT NULL," +
                "UNIQUE (name)" +
                ")"

        suspend fun queryAll() = runWithDb {
            val list = ArrayList<Product>()
            val cursor = SqliteConn.conn.rawQuery("SELECT name, ty, style, spec, dir, hands, color, price FROM $TB_NAME", arrayOf())
            cursor.use {
                while (cursor.moveToNext()) {
                    val product = Product(
                        cursor.getString(0),
                        cursor.getString(1),
                        cursor.getString(2),
                        cursor.getString(3),
                        cursor.getString(4),
                        cursor.getString(5),
                        cursor.getString(6),
                        cursor.getString(7)
                    )
                    list.add(product)
                }
            }
            list
        }

        suspend fun queryWithName(name: String) = runWithDb {
            val cursor = SqliteConn.conn.rawQuery("SELECT name, ty, style, spec, dir, hands, color, price FROM $TB_NAME WHERE name = ? LIMIT 1", arrayOf(name))
            cursor.use {
                if (!cursor.moveToNext()) {
                    throw IllegalStateException(stringById(R.string.mei_you_zhao_dao_gai_chan_pin))
                }

                val product = Product(
                        cursor.getString(0),
                        cursor.getString(1),
                        cursor.getString(2),
                        cursor.getString(3),
                        cursor.getString(4),
                        cursor.getString(5),
                        cursor.getString(6),
                        cursor.getString(7)
                )
                product
            }
        }

        suspend fun delete(name: String) = runWithDb {
            SqliteConn.conn.execSQL("DELETE FROM $TB_NAME WHERE name = ?", arrayOf(name))
        }
    }

    suspend fun saveOnDelete() {
        delete(name)
        save()
    }

    suspend fun save() = runWithDb {
        val cursor = SqliteConn.conn.rawQuery("SELECT style FROM $TB_NAME WHERE name = ?", arrayOf(name))
        val ret = cursor.use {
            cursor.moveToNext()
        }

        if (ret) {
            throw IllegalStateException(stringById(R.string.ming_cheng_zhong_fu_le))
        }

        SqliteConn.conn.execSQL("INSERT INTO $TB_NAME (name, ty, style, spec, dir, hands, color, price) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
                arrayOf(name, ty, style, spec, dir, hands, color, price)
            )
    }
}